/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.siga.modelo.DAO;

import com.siga.modelo.entidad.Semestre;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Ricardo
 */
public class SemestreDAO {
    private EntityManagerFactory emf;
    EntityManager em;

    public SemestreDAO() {
        emf=Persistence.createEntityManagerFactory("AppSigaPPU");
        em=emf.createEntityManager();
    }
    public void closeEntityManager(){
        em.close();
    }
    public boolean insertarSemestre(Semestre s){
        boolean b=false;
        try{
            em.getTransaction().begin();
            em.persist(s);
            em.getTransaction().commit();
            b=true;
        }catch(Exception e){
            em.getTransaction().rollback();
        }
        return b;
    }
    
    public Semestre buscarSemestre(String codigo){
        Semestre s= em.find(Semestre.class, codigo);
        return s;
    }
    
    public List<Semestre> getListaSemestre(){
        Query q=em.createQuery("Select s from Semestre s");//em.createNativeQuery(....);
        return q.getResultList();
    }
    
    public boolean eliminarSemestre(String codigo){
        boolean b=false;
        try{
            em.getTransaction().begin();
            Semestre s= em.find(Semestre.class, codigo);
            em.remove(s);
            em.getTransaction().commit();
            b=true;
        }catch(Exception e){
            em.getTransaction().rollback();
        }
        return b;
    }
}
